import { createRouter, createWebHashHistory } from "vue-router";
import { createLoading } from '@/utils/loading'

const Routes = [
  {
    path: "/home",
    name: "home",
    component: () => import("@/pages/home/index.vue"),
    meta: {
      title: "首页",
    },
  },
  {
    path: "/about",
    name: "about",
    component: () => import("@/pages/about/index.vue"),
    meta: {
      title: "关于",
    },
  },
  {
    path: "/code",
    name: "code",
    component: () => import("@/pages/code/index.vue"),
    meta: {
      title: "代码",
    },
  },
  {
    path: "/posts",
    name: "posts",
    component: () => import("@/pages/posts/index.vue"),
    meta: {
      title: "文稿",
    },
  },
  {
    path: "/timelines",
    name: "timelines",
    component: () => import("@/pages/timelines/index.vue"),
    meta: {
      title: "时光",
    },
  },
  {
    path: "/",
    redirect: "/home",
  },
];
const router = createRouter({
  history: createWebHashHistory(),
  routes: Routes,
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 显示 loading
  createLoading().show()
  // 添加延迟，确保 loading 效果显示足够时间
  setTimeout(() => {
    next()
  }, 800) // 设置 800ms 的延迟
})

// 全局后置守卫
router.afterEach(() => {
  // 添加延迟，确保 loading 效果显示足够时间
  setTimeout(() => {
    createLoading().hide()
  }, 200) // 设置 200ms 的延迟
})

export default router;
